package kata02;

public class RecursiveChop {
    int[] items;

    public RecursiveChop (int[] items) {
        this.items = items;
    }

    public int findIndex (int element) {
        return this.findIndexByRecursive(element, 0, this.items.length - 1);
    }

    private int findIndexByRecursive(int element, int pre, int next) {
        if (pre > next) {
            return -1;
        }
        int middle = (pre + next) / 2;

        if (items[middle] == element) {
            return middle;
        }
        if (items[middle] < element) {
            return findIndexByRecursive(element, middle + 1, next );
        }
        if (items[middle] > element) {
            return findIndexByRecursive(element, pre, middle - 1);
        }
        return -1;
    }
}
